package dongshi.daddy.loadbalance.entity;

import dongshi.daddy.loadbalance.beans.rule.TemplateRule;
import dongshi.daddy.loadbalance.converter.CouponTypeConverter;
import dongshi.daddy.loadbalance.converter.RuleConverter;
import dongshi.daddy.loadbalance.enumm.CouponType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 优惠券模板
 * 表：
 * CREATE TABLE IF NOT EXISTS `geekbang_coupon_db`.`coupon_template` (
 *   `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
 *   `available` BOOLEAN NOT NULL DEFAULT FALSE COMMENT '优惠券可用状态',
 *   `name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '优惠券名称',
 *   `description` VARCHAR(256) NOT NULL DEFAULT '' COMMENT '优惠券详细信息',
 *   `type` VARCHAR(10) NOT NULL DEFAULT '' COMMENT '优惠券类型，比如满减、随机立减、晚间双倍等等',
 *   `shop_id` BIGINT(20) COMMENT '优惠券适用的门店，如果是空则代表全场适用',
 *   `created_time` DATETIME NOT NULL DEFAULT '2021-12-13 00:00:00' COMMENT '创建时间',
 *   `rule` VARCHAR(2000) NOT NULL DEFAULT '' COMMENT '详细的使用规则',
 *   PRIMARY KEY (`id`),
 *   KEY `idx_shop_id` (`shop_id`)
 * ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='优惠券模板';
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Builder
@EntityListeners(AuditingEntityListener.class)
@Table(name = "coupon_template")
public class CouponTemplate implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    // 状态是否可用
    @Column(name = "available", nullable = false)
    private Boolean available;

    @Column(name = "name", nullable = false)
    private String name;

    // 适用门店-如果为空，则为全店满减券
    @Column(name = "shop_id")
    private Long shopId;

    @Column(name = "description", nullable = false)
    private String description;

    // 优惠券类型
    @Column(name = "type", nullable = false)
    @Convert(converter = CouponTypeConverter.class)
    private CouponType category;

    // 创建时间，通过@CreateDate注解自动填值（需要配合@JpaAuditing注解在启动类上生效）
    @CreatedDate
    @Column(name = "created_time", nullable = false)
    private Date createdTime;

    // 优惠券核算规则，平铺成JSON字段
    @Column(name = "rule", nullable = false)
    // 如果数据库中存放的是 code、string、数字等等标记化对象，可以使用 Convert 注解指定一个继承自 AttributeConverter 的类，将 DB 里存的内容转化成一个 Java 对象。
    @Convert(converter = RuleConverter.class)
    private TemplateRule rule;

}
